package com.ruoyi.zxjd.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zxjd.util.AimPathUtils;
import com.ruoyi.zxjd.work.domain.*;
import com.ruoyi.zxjd.work.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 工作进度Controller
 *
 * @author lcc
 * @date 2021-01-26
 */
@Controller
@RequestMapping("/work/progress")
public class WorkProgressController extends BaseController {
    private String prefix = "work/progress";

    @Autowired
    private IWorkProgressService workProgressService;
    @Autowired
    private IWorkFormService workFormService;
    @Autowired
    private IWorkArrangeService workArrangeService;
    @Autowired
    private IWorkTemplateTable1Service workTemplateTable1Service;
    @Autowired
    private IWorkTemplateTable2Service workTemplateTable2Service;
    @Autowired
    private IWorkTemplateTable3Service workTemplateTable3Service;
    @Autowired
    private IWorkTemplateTable4Service workTemplateTable4Service;
    @Autowired
    private IWorkTemplateTable5Service workTemplateTable5Service;
    @Autowired
    private IWorkTemplateTable6Service workTemplateTable6Service;
    @Autowired
    private IWorkTemplateTable7Service workTemplateTable7Service;
    @Autowired
    private IWorkTemplateTable8Service workTemplateTable8Service;
    @Autowired
    private IWorkTemplateTable9Service workTemplateTable9Service;

    @RequiresPermissions("work:progress:view")
    @GetMapping()
    public String progress() {
        return prefix + "/progress";
    }

    /**
     * 查询工作进度列表
     */
    @RequiresPermissions("work:progress:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkProgress workProgress) {
        startPage();
        List<WorkProgress> list = workProgressService.selectWorkProgressList(workProgress);
        return getDataTable(list);
    }

    /**
     * 导出工作进度列表
     */
    @RequiresPermissions("work:progress:export")
    @Log(title = "工作进度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkProgress workProgress) {
        List<WorkProgress> list = workProgressService.selectWorkProgressList(workProgress);
        ExcelUtil<WorkProgress> util = new ExcelUtil<WorkProgress>(WorkProgress.class);
        return util.exportExcel(list, "progress");
    }

    /**
     * 新增工作进度
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工作进度
     */
    @RequiresPermissions("work:progress:add")
    @Log(title = "工作进度", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkProgress workProgress) {
        return toAjax(workProgressService.insertWorkProgress(workProgress));
    }

    /**
     * 修改工作进度
     */
    @GetMapping("/edit/{progressId}")
    public String edit(@PathVariable("progressId") Long progressId, ModelMap mmap) {
        WorkProgress workProgress = workProgressService.selectWorkProgressById(progressId);
        mmap.put("workProgress", workProgress);
        return prefix + "/edit";
    }

    /**
     * 修改保存工作进度
     */
    @RequiresPermissions("work:progress:edit")
    @Log(title = "工作进度", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkProgress workProgress) {
        return toAjax(workProgressService.updateWorkProgress(workProgress));
    }

    /**
     * 删除工作进度
     */
    @RequiresPermissions("work:progress:remove")
    @Log(title = "工作进度", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(workProgressService.deleteWorkProgressByIds(ids));
    }

    /**
     * 查看工作进度详情
     */
    @RequiresPermissions("work:progress:detail")
    @GetMapping("/detail/{progressId}")
    public String detail(@PathVariable("progressId") Long progressId, Long id, String fragment, ModelMap mmap) {
        String aimPath = null;

        WorkProgress workProgress = workProgressService.selectWorkProgressById(progressId);
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(workProgress.getArrangeId());
        mmap.put("workProgress", workProgress);

        mmap.put("progressId", progressId);
        if (1L == workArrange.getTemplateId()) {
            mmap.put("workTemplateTable1List", workTemplateTable1Service.selectWorkTemplateTable1ByProgressId(progressId));
            return "work/templatetable1/edit";
        }
        if (2L == workArrange.getTemplateId()) {
            mmap.put("workTemplateTable2", workTemplateTable2Service.selectWorkTemplateTable2ByProgressId(progressId));
            return "work/templatetable2/edit";
        }
        if (3L == workArrange.getTemplateId()) {
            mmap.put("workTemplateTable3List", workTemplateTable3Service.selectWorkTemplateTable3ByProgressId(progressId));
            return "work/templatetable3/edit";
        }
        if (4L == workArrange.getTemplateId()) {
            mmap.put("workTemplateTable4List", workTemplateTable4Service.selectWorkTemplateTable4ByProgressId(progressId));
            return "work/templatetable4/edit";
        }
        if (5L == workArrange.getTemplateId()) {
            mmap.put("workTemplateTable5List", workTemplateTable5Service.selectWorkTemplateTable5ByProgressId(progressId));
            return "work/templatetable5/edit";
        }
        if (6L == workArrange.getTemplateId()) {
            mmap.put("workTemplateTable6", workTemplateTable6Service.selectWorkTemplateTable6ByProgressId(progressId));
            return "work/templatetable6/edit";
        }
        if (7L == workArrange.getTemplateId()) {

            aimPath = "work/templatetable7/edit";
            WorkTemplateTable7 workTemplateTable7 = null;
            List<WorkTemplateTable7> workTemplateTable7List = workTemplateTable7Service.selectWorkTemplateTable7ByProgressId(progressId);
            mmap.put("workTemplateTable7List", workTemplateTable7List);
            workTemplateTable7 = (WorkTemplateTable7) getWorkTemplateTable(id, workTemplateTable7List, WorkTemplateTable7.class);
            mmap.put("workTemplateTable7", workTemplateTable7);
            mmap.put("createDay", DateUtils.dateTime(workTemplateTable7.getCreateTime()));


            aimPath = AimPathUtils.getAimPath(aimPath, fragment);
            return aimPath;
        }

        if (8L == workArrange.getTemplateId()) {
            aimPath = "work/templatetable8/edit";
            WorkTemplateTable8 workTemplateTable = null;
            List<WorkTemplateTable8> workTemplateTableList = workTemplateTable8Service.selectWorkTemplateTable8ByProgressId(progressId);
            mmap.put("workTemplateTableList", workTemplateTableList);
            workTemplateTable = (WorkTemplateTable8) getWorkTemplateTable(id, workTemplateTableList, WorkTemplateTable7.class);
            mmap.put("workTemplateTable8", workTemplateTable);
            mmap.put("createDay", DateUtils.dateTime(workTemplateTable.getCreateTime()));
            aimPath = AimPathUtils.getAimPath(aimPath, fragment);
            return aimPath;
        }

        if (9L == workArrange.getTemplateId()) {
            aimPath = "work/templatetable9/edit";
            WorkTemplateTable9 workTemplateTable = null;
            List<WorkTemplateTable9> workTemplateTableList = workTemplateTable9Service.selectWorkTemplateTable9ByProgressId(progressId);
            mmap.put("workTemplateTableList", workTemplateTableList);
            workTemplateTable = (WorkTemplateTable9) getWorkTemplateTable(id, workTemplateTableList, WorkTemplateTable9.class);
            mmap.put("workTemplateTable9", workTemplateTable);
            mmap.put("createDay", DateUtils.dateTime(workTemplateTable.getCreateTime()));
            aimPath = AimPathUtils.getAimPath(aimPath, fragment);
            return aimPath;
        }
        return prefix + "/detail";
    }


    /**
     * 接收工作任务
     */
    @RequiresPermissions("work:progress:receive")
    @Log(title = "接收工作任务", businessType = BusinessType.UPDATE)
    @PostMapping("/receive")
    @ResponseBody
    public AjaxResult receive(Long progressId) {
        return toAjax(workProgressService.receiveTask(progressId));
    }

    /**
     * 跳转工作填报页面
     */
    @RequiresPermissions("work:progress:fill")
    @GetMapping("/fill_in/{progressId}")
    public String fullIn(@PathVariable("progressId") Long progressId, String fragment, Long id, ModelMap mmap) {
        String aimPath = null;
        WorkProgress workProgress = workProgressService.selectWorkProgressById(progressId);
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(workProgress.getArrangeId());
        mmap.put("today", DateUtils.getDate());
        mmap.put("workProgress", workProgress);
        if (1L == workArrange.getTemplateId()) {
            return "work/templatetable1/add";
        }
        if (2L == workArrange.getTemplateId()) {
            WorkTemplateTable2 workTemplateTable2 = workTemplateTable2Service.selectWorkTemplateTable2ByProgressId(progressId);
            mmap.put("workTemplateTable2", workTemplateTable2 == null ? new WorkTemplateTable2() : workTemplateTable2);
            return "work/templatetable2/add";
        }
        if (3L == workArrange.getTemplateId()) {
            return "work/templatetable3/add";
        }
        if (4L == workArrange.getTemplateId()) {
            return "work/templatetable4/add";
        }
        if (5L == workArrange.getTemplateId()) {
            return "work/templatetable5/add";
        }
        if (6L == workArrange.getTemplateId()) {
            WorkTemplateTable6 workTemplateTable6 = workTemplateTable6Service.selectWorkTemplateTable6ByProgressId(progressId);
            mmap.put("workTemplateTable6", workTemplateTable6 == null ? new WorkTemplateTable6() : workTemplateTable6);
            return "work/templatetable6/add";
        }

        if (7L == workArrange.getTemplateId()) {
            aimPath = "work/templatetable7/add";
            WorkTemplateTable7 workTemplateTable7 = null;
            List<WorkTemplateTable7> workTemplateTable7List = workTemplateTable7Service.selectWorkTemplateTable7ByProgressId(progressId);
            mmap.put("workTemplateTable7List", workTemplateTable7List);

            workTemplateTable7 = (WorkTemplateTable7) getWorkTemplateTable(id, workTemplateTable7List, WorkTemplateTable7.class);

            mmap.put("workTemplateTable7", workTemplateTable7);

            aimPath = AimPathUtils.getAimPath(aimPath, fragment);

            return aimPath;
        }

        if (8L == workArrange.getTemplateId()) {

            aimPath = "work/templatetable8/add";
            WorkTemplateTable8 workTemplateTable = null;
            List<WorkTemplateTable8> workTemplateTableList = workTemplateTable8Service.selectWorkTemplateTable8ByProgressId(progressId);
            mmap.put("workTemplateTableList", workTemplateTableList);

            workTemplateTable = (WorkTemplateTable8) getWorkTemplateTable(id, workTemplateTableList, WorkTemplateTable8.class);

            mmap.put("workTemplateTable8", workTemplateTable);
            aimPath = AimPathUtils.getAimPath(aimPath, fragment);
            return aimPath;
        }

        if (9L == workArrange.getTemplateId()) {
            aimPath = "work/templatetable9/add";
            WorkTemplateTable9 workTemplateTable = null;
            List<WorkTemplateTable9> workTemplateTableList = workTemplateTable9Service.selectWorkTemplateTable9ByProgressId(progressId);
            mmap.put("workTemplateTableList", workTemplateTableList);
            workTemplateTable = (WorkTemplateTable9) getWorkTemplateTable(id, workTemplateTableList, WorkTemplateTable9.class);

            mmap.put("workTemplateTable9", workTemplateTable);
            aimPath = AimPathUtils.getAimPath(aimPath, fragment);
            return aimPath;
        }
        return prefix + "/fill_in";
    }


    /**
     * 根据集合和id的关系,(是否为空,是由有对应id的对象)获取集合中的指定
     *
     * @param id
     * @param list
     * @return
     */
    private BaseWorkTemplateTable getWorkTemplateTable(Long id, List<? extends BaseWorkTemplateTable> list, Class<? extends BaseWorkTemplateTable> aclass) {

        BaseWorkTemplateTable t = null;

        try {
            if (list == null || list.size() == 0 || (id != null &&  id == -1)) {

                t = aclass.newInstance();

            } else if (id == null) {
                t = list.get(0);
            }  else {
                List<BaseWorkTemplateTable> collect = list.stream().filter(u -> u.getId().equals(id)).collect(Collectors.toList());
                t = collect.size() == 1 ? collect.get(0) : aclass.newInstance();
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }


    /**
     * 跳转工作填报页面
     */
    @RequiresPermissions("work:progress:fill")
    @GetMapping("/fill_in_edit/{progressId}/{id}")
    public String fullInEdit(@PathVariable("progressId") Long progressId, @PathVariable("id") Long id, String fragment, ModelMap mmap) {
        String aimPath = null;
        WorkProgress workProgress = workProgressService.selectWorkProgressById(progressId);
        WorkArrange workArrange = workArrangeService.selectWorkArrangeById(workProgress.getArrangeId());
        mmap.put("workProgress", workProgress);
        mmap.put("today", DateUtils.getDate());
        if (7L == workArrange.getTemplateId()) {
            List<WorkTemplateTable7> workTemplateTable7List = workTemplateTable7Service.selectWorkTemplateTable7ByProgressId(progressId);
            mmap.put("workTemplateTable7List", workTemplateTable7List);
            WorkTemplateTable7 workTemplateTable7 = getWorkTemplateTable7(progressId, id);
            mmap.put("workTemplateTable7", workTemplateTable7);

            aimPath = AimPathUtils.getAimPath(aimPath, fragment);

            return aimPath;
        }
        return prefix + "/fill_in";
    }


    /**
     * 根据id的值,确定返回的不同的模板对象
     *
     * @param progressId 任务进度id
     * @param id         模板对象id
     * @return 模板对象
     */
    private WorkTemplateTable7 getWorkTemplateTable7(Long progressId, Long id) {

        WorkTemplateTable7 workTemplateTable7 = null;
        if (id == null) {
            workTemplateTable7 = workTemplateTable7Service.selectWorkTemplateTableByProgressIdLimit1(progressId);
        } else if (id > 0) {
            workTemplateTable7 = workTemplateTable7Service.selectWorkTemplateTable7ById(id);
        }

        return workTemplateTable7 == null ? new WorkTemplateTable7() : workTemplateTable7;
    }


    /**
     * 根据id的值,确定返回的不同的模板对象
     *
     * @param progressId 任务进度id
     * @param id         模板对象id
     * @return 模板对象
     */
    private WorkTemplateTable8 getWorkTemplateTable8(Long progressId, Long id) {

        WorkTemplateTable8 workTemplateTable8 = null;
        if (id == null) {
            workTemplateTable8 = workTemplateTable8Service.selectWorkTemplateTableByProgressIdLimit1(progressId);
        } else if (id > 0) {
            workTemplateTable8 = workTemplateTable8Service.selectWorkTemplateTable8ById(id);
        }

        return workTemplateTable8 == null ? new WorkTemplateTable8() : workTemplateTable8;
    }


    /**
     * 根据id的值,确定返回的不同的模板对象
     *
     * @param progressId 任务进度id
     * @param id         模板对象id
     * @return 模板对象
     */
    private WorkTemplateTable9 getWorkTemplateTable9(Long progressId, Long id) {

        WorkTemplateTable9 workTemplateTable9 = null;
        if (id == null) {
            workTemplateTable9 = workTemplateTable9Service.selectWorkTemplateTableByProgressIdLimit1(progressId);
        } else if (id > 0) {
            workTemplateTable9 = workTemplateTable9Service.selectWorkTemplateTable9ById(id);
        }

        return workTemplateTable9 == null ? new WorkTemplateTable9() : workTemplateTable9;
    }

    /**
     * 保存填报表单数据
     */
    @RequiresPermissions("work:progress:form")
    @Log(title = "保存填报数据", businessType = BusinessType.UPDATE)
    @PostMapping("/form")
    @ResponseBody
    public AjaxResult editFormSave(WorkForm workForm) {
        return toAjax(workFormService.updateWorkForm(workForm));
    }

    /**
     * 上报工作任务
     */
    @RequiresPermissions("work:progress:submit")
    @Log(title = "工作进度", businessType = BusinessType.DELETE)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(String ids) {
        return toAjax(workProgressService.updateWorkProgressStatusByIds(ids));
    }

    /**
     * 待办任务
     */
    @GetMapping("/to_done_task")
    public String toDoneTask(ModelMap mmap) {
        return prefix + "/personal/task_to_done";
    }

    /**
     * 已办任务
     */
    @GetMapping("/done_task")
    public String doneTask(ModelMap mmap) {
        return prefix + "/personal/task_done";
    }

    /**
     * 逾期任务
     */
    @GetMapping("/overdue_task")
    public String overdueTask(ModelMap mmap) {
        return prefix + "/personal/task_overdue";
    }

    /**
     * 退回重填
     */
    @RequiresPermissions("work:progress:refill")
    @Log(title = "退回重填", businessType = BusinessType.UPDATE)
    @PostMapping("/refill")
    @ResponseBody
    public AjaxResult refill(Long progressId) {
        return toAjax(workProgressService.refillTask(progressId));
    }

    /**
     * 短信提示
     */
    @RequiresPermissions("work:progress:sms")
    @Log(title = "短信提示", businessType = BusinessType.UPDATE)
    @PostMapping("/smsPrompt")
    @ResponseBody
    public AjaxResult sms(Long progressId) {
        return toAjax(workProgressService.smsPrompt(progressId));
    }


  /*  *//**
     * 加载任务处理列表树
     * 根据工作进度id progressId 查询
     *//*
    @GetMapping("/treeData/{progressId}")
    @ResponseBody
    public List<Ztree> treeData(@PathVariable("progressId") Long progressId) {
//        List<Ztree> ztrees = workProgressService.selectTemplateTree(progressId);
//        return ztrees;
        return null;
    }*/


}
